import { queryWinDate } from "@/api/chkNum";
interface DateItem {
  draw_date: string;
  draw_id: string | number;
}
const useDateList = defineStore("dateList", () => {
  const dateList = ref<DateItem[]>([]);
  const curDate = ref();
  const showDateList = ref(false);
  const dateParams = reactive({
    draw_year: ""
  });
  function toggleShowSelDate() {
    showDateList.value = !showDateList.value;
  }
  function handleSelDate(it: any) {
    curDate.value = it.draw_date;
    toggleShowSelDate();
  }

  function findDrawIdByDate(draw_date: string): string | undefined {
    const dateItem = dateList.value.find(item => item.draw_date === draw_date);
    return dateItem ? String(dateItem.draw_id) : undefined;
  }
  const fetchDateList1 = async () => {
    const res = await queryWinDate(dateParams);
    dateList.value = res?.data ?? [];
    curDate.value = dateList.value[0]?.draw_date;
  };
  return {
    dateList,
    curDate,
    showDateList,
    toggleShowSelDate,
    handleSelDate,
    findDrawIdByDate,
    fetchDateList1,
    dateParams
  };
});
export const useDateListRefs = () => storeToRefs(useDateList());
export default useDateList;
